package cn.gson.school.controller.jiuye;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiuye.InterviewExperienceEntityDao;
import cn.gson.school.dao.jiuye.InterviewMaterialsEntityDao;
import cn.gson.school.entity.jiuye.InterviewExperienceEntity;
import cn.gson.school.entity.jiuye.InterviewMaterialsEntity;
import cn.gson.school.mappers.jiuye.InterviewExperienceEntityMapper;
import cn.gson.school.mappers.jiuye.InterviewMaterialsEntityMapper;

@Controller
@RequestMapping("/ziliao")
public class InterviewExperienceEntityController {
	@Autowired
	private InterviewMaterialsEntityDao interviewMaterialsEntityDao;
	@Autowired
	private InterviewMaterialsEntityMapper interviewMaterialsEntityMapper;


	/***
	 * 面试资料表
	 * @param interviewMaterialsEntity
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("ziliao")
	@ResponseBody
	public Map<String, Object> index(InterviewMaterialsEntity interviewMaterialsEntity,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 配置分页
		PageHelper.startPage(pageNum, pageSize);
		// 查询数据
		List<InterviewMaterialsEntity> listDate = interviewMaterialsEntityMapper.list(interviewMaterialsEntity);
		PageInfo<InterviewMaterialsEntity> info = new PageInfo<>(listDate);
		
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	@GetMapping({ "jy-ms-tjjingyan", "jy-ms-tjjingyan/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model,
			@Valid InterviewExperienceEntity interviewExperienceEntity, BindingResult br) {
		if (id != null) {
			InterviewMaterialsEntity u = interviewMaterialsEntityDao.findOne(id);

			if (u != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				String a = u.getInterviewMaterialsAnswer();
				String q = u.getInterviewMaterialsQuestion();
				String t = u.getInterviewQuestionType();
				model.addAttribute("answer", a);
				model.addAttribute("question", q);
				model.addAttribute("type", t);
			}
		}
		return "jiuye/jy-ms-tjjingyan";
	}

	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		InterviewMaterialsEntity a=interviewMaterialsEntityDao.findOne(id);
		a.setInterviewMaterialsExperience(1);
		interviewMaterialsEntityDao.save(a);
		obj.put("success", true);
		return obj;
	}

}
